package cn.bsuc.crm.service.impl;

import cn.bsuc.crm.dto.QueryLogParamsDTO;
import cn.bsuc.crm.entity.Log;
import cn.bsuc.crm.mapper.LogMapper;
import cn.bsuc.crm.service.LogService;
import cn.bsuc.crm.util.Result;
import cn.bsuc.crm.vo.ExportLogVO;
import cn.hutool.core.lang.Dict;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhuoyuan
 * @since 2022-10-03
 */
@Service
@Slf4j
public class LogServiceImp extends ServiceImpl<LogMapper, Log> implements LogService {

    @Override
    public Result findAllLog(QueryLogParamsDTO queryLogParamsDTO) {
        Page<Log> logPage = new Page<>(queryLogParamsDTO.getCurrentPage(), queryLogParamsDTO.getPageSize());

        String createMan = queryLogParamsDTO.getCreateMan();
        Integer exceptionCode = queryLogParamsDTO.getExceptionCode();
        String description = queryLogParamsDTO.getDescription();

        LambdaQueryWrapper<Log> lqw = new LambdaQueryWrapper<>();
        lqw.like(description != null && !description.equals(""), Log::getDescription, description)
                .like(createMan != null && !createMan.equals(""), Log::getCreateMan, createMan)
                .eq(exceptionCode != null && exceptionCode == 2000, Log::getExceptionCode, exceptionCode)
                .ne(exceptionCode != null && exceptionCode != 2000, Log::getExceptionCode, 2000)
                .orderByDesc(Log::getCreateDate);

        Page<Log> logs = baseMapper.selectPage(logPage, lqw);

        return Result.ok(logs);
    }

    @Override
    public Result deleteLog(Integer[] ids) {
        List<Integer> list = Arrays.stream(ids).collect(Collectors.toList());
        baseMapper.deleteBatchIds(list);
        return Result.ok();
    }

    @Override
    public Result cleanLog() {
        baseMapper.delete(null);
        return Result.ok();
    }

    @Override
    public Result exportLog(HttpServletResponse response) {
        //设置下载信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = "crm_log";
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //查询数据库
        List<Log> logList = baseMapper.selectList(null);
        //Log -- ExportLogVO
        List<ExportLogVO> exportLogList = new ArrayList<>();
        for (Log log : logList) {
            ExportLogVO dictEeVo = new ExportLogVO();
            BeanUtils.copyProperties(log, dictEeVo);
            exportLogList.add(dictEeVo);
        }
        //调用方法进行写操作
        try {
            EasyExcel.write(response.getOutputStream(), ExportLogVO.class).sheet("log")
                    .doWrite(exportLogList);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.ok();
    }
}
